#!/usr/bin/env python
# -*- coding: utf-8 -*-

from core.utils.singleton import singleton
from core.event.eventdispatcher import EventDispatcher
from core.ide.datamangager import DataMangager
import wx    

    #MENU=Menu()
    #MAINFRAME=MainFrame

@singleton
class Core(EventDispatcher):
    def __init__(self):
        EventDispatcher.__init__(self)
        

    def launch(self):
        self._init_window()
        self._init_centralpanel()
        self._init_menu()
        self._init_toolbar()
        self._init_extension()
        self._init_statusbar()
        
        self._ready()

    def _init_window(self):
        from core.ui.mainframe import MainFrame
        self._mainFrame = MainFrame()
        self._mainFrame.Bind(wx.EVT_CLOSE,self.onClose)
        
    def onClose(self,e=None):
        from core.ide.langsmanager import LangsManager
        LangsManager().destroy()
        self._mainFrame.Destroy()
        if e:
            e.Skip()

    def _init_centralpanel(self):
        from core.ui.centralpanel import CentralPanel
        import core.ui.mainframe as pane
        self._mainPanel = CentralPanel(self._mainFrame)
        self._mainFrame.addPane(self._mainPanel, pane.PANE_MASTER)
    
    def _init_menu(self):
        from core.ui.menu import MainMenu
        self._menu=MainMenu(self._mainFrame)
        self._mainFrame.SetMenuBar(self._menu)
        self._menu.quickMenu("File","|Exit","Exit")
        self._menu.quickMenu('Edit')
        self._menu.quickMenu('Tools')
        self._menu.quickMenu('Help')
        
    def _init_statusbar(self):
        from core.ui.statusbar import Statusbar
        self._statusbar=Statusbar(self._mainFrame)
        self._mainFrame.SetStatusBar(self._statusbar)
        #self._mainFrame.CreateStatusBar()
        #self._mainFrame.GetStatusBar().SetStatusText("Ready")

    def _init_extension(self):
        from extensions import Extension
        self._extension=Extension()
        self._extension.build()

    def _init_toolbar(self):
        from core.ui.toolsbar import ToolsBarManager
        self._toolbar=ToolsBarManager()

    def _ready(self):
        from core.event.event import Event
        self._mainFrame.Show(True)
        
        self._menu.build()
        self._toolbar.createToolsBar("Main",'save','new_doc','|','back','next')
        #DataMangager().setData('druid','Debug',True)
        self.dispatchEvent(Event(Event.READY))

    def getMainFrame(self):
        return self._mainFrame
    
    def getCentralPanel(self):
        return self._mainPanel
    
    def getMenu(self):
        return self._menu